package com.sunwayworld.basemodule.business.outsourcers.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.outsourcers.dao.LimsOutsourcersDao;
import com.sunwayworld.basemodule.business.outsourcers.bean.LimsOutsourcersBean;
import com.sunwayworld.basemodule.business.outsourcers.service.LimsOutsourcersService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsOutsourcersServiceImpl implements LimsOutsourcersService {

    @Autowired
    private LimsOutsourcersDao limsOutsourcersDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsOutsourcersDao getDao() {
        return limsOutsourcersDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsOutsourcersBean limsOutsourcers = jsonWrapper.parseUnique(LimsOutsourcersBean.class);
        limsOutsourcers.setId(ApplicationContextHelper.getNextIdentity());

//        limsOutsourcers.setOutsourcerCode(ApplicationContextHelper.getNextSequence(getDao().getTable()));

        getDao().insert(limsOutsourcers);
        return limsOutsourcers.getId();
    }

    @Override
    @Transactional
    public JSONObject setActivedFlag(RestJsonWrapperBean wrapperBean) {
        JSONObject req = new JSONObject();
        String status = wrapperBean.getParamValue("status");
        List<LimsOutsourcersBean> beans = wrapperBean.parse(LimsOutsourcersBean.class);
        if (StringUtils.isBlank(status) || beans.isEmpty()) {
            return req;
        }
        LimsOutsourcersBean update = new LimsOutsourcersBean();
        update.setActivedFlag(status);
        List<Long> ids = beans.stream().map(LimsOutsourcersBean::getId).collect(Collectors.toList());
        getDao().updateByIds(update, ids, "activedFlag");
        return null;
    }
}
